Global Variables

The following global variables are available globally.

  • Listener filter callback that always returns true. When using this filter callback the Listener will always be notified.

    Declaration

    Swift

    public let alwaysFilter: FilterFunction<Any> = { (oldSubState: Any, newSubState: Any) in
  • Listener filter callback that returns true if the old state and the new state are not equal. You can use this filter function when adding a listener if you want your notification function to be called when the state changes.

    In order to use this filter block your state types has to implement SuasDynamicEquatable protocol Note: if you implement Equatable you can implement SuasDynamicEquatable without any extra code. You only have to include SuasDynamicEquatable in the list of protocols for your type (check examples).

    Example

    Implementing SuasDynamicEquatable manually

    Implementing SuasDynamicEquatable without Equatable

    // Implement SuasDynamicEquatable manually
    struct MyState: SuasDynamicEquatable {
      let value: Int
    
      func isEqual(to other: Any) -> Bool {
        // Cast to same type
        guard let other = other as? MyState else { return false }
    
        // Compare values
        return other.value == self.value
      }
    }
    
    let subscription = store.addListener(forStateType: MyState.self, if: EqualsFilter) { newState in
      // use new state
    }
    

    Implementing SuasDynamicEquatable as an extension

    If your type implement equatable

    struct MyState: Equatable {
      let value: Int
      static func ==(lhs: MyState, rhs: MyState) -> Bool { ... }
    }
    

    You dont need to implement SuasDynamicEquatable just add it as an extension to MyState. No extra code needed.

    extension MyState: SuasDynamicEquatable { }
    

    EqualsFilter now works with MyState

    let subscription = store.addListener(forStateType: MyState.self, if: EqualsFilter) { newState in
      // use new state
    }
    

    Declaration

    Swift

    public let EqualsFilter: FilterFunction<Any> = { (oldSubState: Any, newSubState: Any) in